package com.ruoyi.abuwxapi;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.GardenFlower;
import com.ruoyi.system.service.IGardenFlowerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 花朵信息Controller
 * 
 * @author ruoyi
 * @date 2025-01-02
 */
@RestController
@RequestMapping("/wxapi/flower")
public class GardenFlowerController extends BaseController
{
    @Autowired
    private IGardenFlowerService gardenFlowerService;

    /**
     * 查询花朵信息列表
     */
//    @PreAuthorize("@ss.hasPermi('system:flower:list')")
//    @GetMapping("/list")
//    public TableDataInfo list(GardenFlower gardenFlower)
//    {
//        gardenFlowerService.initOrUpdateUserGarden(gardenFlower.getWxuserId());
//        startPage();
//        List<GardenFlower> list = gardenFlowerService.selectGardenFlowerList(gardenFlower);
//        return getDataTable(list);
//    }
    @GetMapping("/list")
    public TableDataInfo list(GardenFlower gardenFlower)
    {
        // 1. 先初始化/更新花园，获取新增花朵信息
        Map<String, Object> updateResult = gardenFlowerService.initOrUpdateUserGarden(gardenFlower.getWxuserId());

        // 2. 获取当前月份的花朵列表
        startPage();
        List<GardenFlower> list = gardenFlowerService.selectGardenFlowerList(gardenFlower);

        // 3. 创建一个包含额外信息的Map
        Map<String, Object> result = new HashMap<>();
        result.put("flowers", list);
        result.put("totalNewFlowers", updateResult.get("totalNewFlowers"));
        result.put("newFlowersByMonth", updateResult.get("newFlowersByMonth"));

        // 4. 将Map转为List
        List<Map<String, Object>> resultList = new ArrayList<>();
        resultList.add(result);

        return getDataTable(resultList);
    }
    /**
     * 导出花朵信息列表
     */
//    @PreAuthorize("@ss.hasPermi('system:flower:export')")
    @Log(title = "花朵信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GardenFlower gardenFlower)
    {
        List<GardenFlower> list = gardenFlowerService.selectGardenFlowerList(gardenFlower);
        ExcelUtil<GardenFlower> util = new ExcelUtil<GardenFlower>(GardenFlower.class);
        util.exportExcel(response, list, "花朵信息数据");
    }

    /**
     * 获取花朵信息详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:flower:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(gardenFlowerService.selectGardenFlowerById(id));
    }

    /**
     * 新增花朵信息
     */
//    @PreAuthorize("@ss.hasPermi('system:flower:add')")
    @Log(title = "花朵信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GardenFlower gardenFlower)
    {
        return toAjax(gardenFlowerService.insertGardenFlower(gardenFlower));
    }

    /**
     * 修改花朵信息
     */
//    @PreAuthorize("@ss.hasPermi('system:flower:edit')")
    @Log(title = "花朵信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GardenFlower gardenFlower)
    {
        return toAjax(gardenFlowerService.updateGardenFlower(gardenFlower));
    }

    /**
     * 删除花朵信息
     */
//    @PreAuthorize("@ss.hasPermi('system:flower:remove')")
    @Log(title = "花朵信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(gardenFlowerService.deleteGardenFlowerByIds(ids));
    }
}
